package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateAnalyseListDTO;
import com.itlong.cloud.POJO.VO.operate.OperateAnalyseListVO;
import com.itlong.cloud.operate.dao.IOperateAnalyseDao;
import com.itlong.cloud.operate.service.IOperateAnalyseService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-运营分析业务处理接口
 * </desc>
 *
 * @createDate 2019/06/03
 */
@Service
public class OperateAnalyseServiceImpl implements IOperateAnalyseService{
    @Autowired
    IOperateAnalyseDao iOperateAnalyseDao;
    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param dto
     * @return
     * @author Jiaqi.X
     * @createDate 2019/06/03
     */
    @Override
    public Map<String,Object> list(OperateAnalyseListDTO dto) throws Exception {
        Map<String,Object> result = new HashMap<>();
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(dto);
        //分页类默认pageSize为20
        Page<OperateAnalyseListVO> page = new Page<>();
        page.getPage().setPageSize(dto.getPageSize());
        page.getPage().setCurrentPage(dto.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        if(dto.getExport()!=null && dto.getExport()==1){
            params.put("pageSize",2000);
        }else {
            params.put("pageSize", page.getPage().getPageSize());
        }
        //填充返回数据集
        page.setRows(iOperateAnalyseDao.getPage(params), iOperateAnalyseDao.getPageCount(params));
        List<OperateAnalyseListVO> list = page.getRows();
        BigDecimal dueTotal = new BigDecimal("0.00");
        BigDecimal paidTotal = new BigDecimal("0.00");
        BigDecimal shareTotal = new BigDecimal("0.00");
        String colectionRate = "0";
        for(OperateAnalyseListVO vo : list) {
            if (StringUtils.isNotBlank(vo.getRoomTotal()) && StringUtils.isNotBlank(vo.getHouseholdTotal())) {
                vo.setInRate(new BigDecimal(vo.getHouseholdTotal()).divide(new BigDecimal(vo.getRoomTotal()), 4, BigDecimal.ROUND_DOWN).
                        multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_DOWN).toString() + "%");
            }
            if (StringUtils.isNotBlank(vo.getDueAmount())) {
                //应收总额
                dueTotal = dueTotal.add(new BigDecimal(vo.getDueAmount()));
            }
            if (StringUtils.isNotBlank(vo.getPaidAmount())) {
                //实缴总额
                paidTotal = paidTotal.add(new BigDecimal(vo.getPaidAmount()));
            }
            if (StringUtils.isNotBlank(vo.getShareTotal())) {
                //分润总额
                shareTotal = shareTotal.add(new BigDecimal(vo.getShareTotal()));
            }
        }
        if(!dueTotal.toString().equals("0.00") && !paidTotal.toString().equals("0.00")){
            colectionRate = paidTotal.divide(dueTotal,4,BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_DOWN).toString();
        }
        result.put("page",page.getPage());
        result.put("rows",page.getRows());
        result.put("dueTotal",dueTotal.toString());
        result.put("paidTotal",paidTotal.toString());
        result.put("shareTotal",shareTotal.toString());
        result.put("colectionRate",colectionRate);
        return result;
    }

}
